//JS初始化;
$(function() {
	//初始化面板;
	initPanelUI();
	//初始化按钮;
	initBtnUI();
	//初始化窗口;
	initWindowUI();
	//初始数据表格;
	initDataGridUI();
	

});

//初始化面板
function initPanelUI(){
	//初始化页面面板
	$('#dictcatalogPanel').panel({
		border:false
	});
	//初始化窗口面板
	$('#dictcatalogWindowPanel').panel({
		border:false
	});
	//初始化浏览面板
	$('dictcatalogViewPanel').panel({
		border:false
	})
}

//初始化BTN
function initBtnUI(){
	$('#addDictcatalogBtn').linkbutton({plain:true,iconCls:'icon-add'});
	$('#viewDictcatalogBtn').linkbutton({plain:true,iconCls:'icon-view'});
	$('#editDictcatalogBtn').linkbutton({plain:true,iconCls:'icon-edit'});
	$('#delDictcatalogBtn').linkbutton({plain:true,iconCls:'icon-remove'});
	$('#closeDictcatalogBtn').linkbutton({plain:true,iconCls:'icon-close'});

	//增加编辑模块BTN
	$('#saveDictcatalogBtn').linkbutton({plain:true,iconCls:'icon-save'});
	$('#closeDictcatalogWindowBtn').linkbutton({plain:true,iconCls:'icon-close'});
	
	//数据字典增加编辑
	$('#addDictionaryBtn').linkbutton({plain:true,iconCls:'icon-add'});
	$('#editDictionaryBtn').linkbutton({plain:true,iconCls:'icon-edit'});
	$('#removeDictionaryBtn').linkbutton({plain:true,iconCls:'icon-remove'});
	$('#saveDictionaryBtn').linkbutton({plain:true,iconCls:'icon-save'});
	$('#closeDictionaryBtn').linkbutton({plain:true,iconCls:'icon-close'});
}

//初始化窗口
function initWindowUI(){
	//初始化增加编辑窗口
	$('#dictcatalogWindow').window({
		title:'',
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-mail-open',
		width:650,
		height:500
	});
	//初始化浏览窗口
	$('#dictcatalogViewWindow').window({
		title:'浏览',
		closed:true,
		modal:true,
		closable:true,
		minimizable:false,
		maximizable:false,
		collapsible:false,
		resizable:false,
		iconCls:'icon-view',
		width:450,
		height:300
	});
}

//初始化数据表
function initDataGridUI(){
	//初始化字典目录数据表格
	$('#dictcatalogDataGrid').datagrid({
		url:'dictcatalogAjax!loadDictcatalogData',
		nowrap: false,
		border:false,
		striped: true,
		collapsible:true,
		sortName: 'id',
		singleSelect:true,
		sortOrder: 'desc',
		remoteSort: false,
		rownumbers: true,
		pagination:true,
		idField:'id',
		columns:[[
			{field:'dictName',title:'字典目录',width:200, editor:'text'},
			{field:'code',title:'代码',width:200, editor:'text'},
			{field:'des',title:'描述',width:300, editor:'text'}
		]]
	});

	//分页
	var pager = $('#dictcatalogDataGrid').datagrid('getPager');
	$(pager).pagination({
		pageSize: 15,
		pageList: [15, 30, 50, 100],
		beforePageText: '第',
		afterPageText: '页    共 {pages} 页',
		displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
		onSelectPage:function(pageNumber, pageSize){
			$('#dictcatalogDataGrid').datagrid('options').url = 'dictcatalogAjax!loadDictcatalogData?pageNo=' + pageNumber + '&pageSize='+ pageSize;
			$('#dictcatalogDataGrid').datagrid('reload', pageNumber);
		}
	});
	
	
	//初始化字典目录数据表格
	$('#dictionaryDataGrid').datagrid({
		url:'dictionaryAjax!loadDictionaryData',
		nowrap: false,
		border:false,
		striped: true,
		collapsible:true,
		sortName: 'id',
		singleSelect:true,
		sortOrder: 'asc',
		remoteSort: false,
		rownumbers: true,
		idField:'id',
		columns:[[
			{field:'key', title:'Key', width:100, editor:'text'},
			{field:'word', title:'Value', width:200, editor:'text'}
		]]
	});

}


function openDictcatalogWindow(type){
	if(type == 'add'){
		dictcatalogAdd();
		return;
	}else if(type == 'save'){
		dictcatalogSave();
		return;
	}else{
		var node = $('#dictcatalogDataGrid').datagrid('getSelected');
		if(node == null){
			$.messager.alert('提示','请先选择与一条记录!');
			return;
		}else{
			if(type == 'view'){
				$('#dictionaryDataGrid').datagrid('options').url = 'dictionaryAjax!loadDictionaryData?dclId=' + node.id; 
				$('#dictionaryDataGrid').datagrid('reload');
				$('#dictcatalogViewWindow').window('open');
			}else if(type == 'edit'){
				dictcatalogEdit();
				return;
			}else if(type == 'del'){
				dictcatalogRemove();
				return;
			}else{
				$.messager.alert('提示','非法操作!');
				return;
			}
		}

	}
	
}


/**
 * 字典目录维护
 */
function dictcatalogAdd(){
	$('#dictcatalogDataGrid').datagrid('endEdit', lastIndex);
	$('#dictcatalogDataGrid').datagrid('appendRow',{
		id:'',
		dictName:'',
		code:'',
		des:''
	});	
	var lastIndex = $('#dictcatalogDataGrid').datagrid('getRows').length-1;
	$('#dictcatalogDataGrid').datagrid('selectRow', lastIndex);
	$('#dictcatalogDataGrid').datagrid('beginEdit', lastIndex);
}

function dictcatalogEdit(){
	var row = $('#dictcatalogDataGrid').datagrid('getSelected');
	if (row) {
		var rowIndex = $('#dictcatalogDataGrid').datagrid('getRowIndex', row);
		$('#dictcatalogDataGrid').datagrid('beginEdit', rowIndex);
	}
}

function dictcatalogRemove(){
	var row = $('#dictcatalogDataGrid').datagrid('getSelected');
	if (row) {
		var rowIndex = $('#dictcatalogDataGrid').datagrid('getRowIndex', row);
		$('#dictcatalogDataGrid').datagrid('deleteRow', rowIndex);
	}
}

function dictcatalogSave(){
	var rows = $('#dictcatalogDataGrid').datagrid('getRows');
	for ( var i = 0; i < rows.length; i++) {
		$('#dictcatalogDataGrid').datagrid('endEdit', i);
	}
	
	if ($('#dictcatalogDataGrid').datagrid('getChanges').length) {
		var inserted = $('#dictcatalogDataGrid').datagrid('getChanges', "inserted");
		var deleted = $('#dictcatalogDataGrid').datagrid('getChanges', "deleted");
		var updated = $('#dictcatalogDataGrid').datagrid('getChanges', "updated");
		var effectRow = new Object();
		
		if (inserted.length) {
			effectRow["inserted"] = JSON.stringify(inserted);
			
		}
		if (deleted.length) {
			effectRow["deleted"] = JSON.stringify(deleted);
		}
		if (updated.length) {
			effectRow["updated"] = JSON.stringify(updated);
		}
		
		$.post("dictcatalogAjax!save", effectRow,
				function(data) {
					$.messager.alert("提示", "保存成功!");
		}, "json").error(function() {
			$.messager.alert("提示", "保存失败!");
		});
		$('#dictcatalogDataGrid').datagrid('reload');
	}
}

/**
 * 字典键值维护
 */
function dictionaryAdd(){
	var node = $('#dictcatalogDataGrid').datagrid('getSelected');
	$('#dictionaryDataGrid').datagrid('endEdit', lastIndex);
	$('#dictionaryDataGrid').datagrid('appendRow',{
		id:'',
		key:'',
		word:'',
		dclId:node.id
	});	
	var lastIndex = $('#dictionaryDataGrid').datagrid('getRows').length-1;
	$('#dictionaryDataGrid').datagrid('selectRow', lastIndex);
	$('#dictionaryDataGrid').datagrid('beginEdit', lastIndex);
}

function dictionaryEdit(){
	var row = $('#dictionaryDataGrid').datagrid('getSelected');
	if (row) {
		var rowIndex = $('#dictionaryDataGrid').datagrid('getRowIndex', row);
		$('#dictionaryDataGrid').datagrid('beginEdit', rowIndex);
	}
}

function dictionaryRemove(){
	var row = $('#dictionaryDataGrid').datagrid('getSelected');
	if (row) {
		var rowIndex = $('#dictionaryDataGrid').datagrid('getRowIndex', row);
		$('#dictionaryDataGrid').datagrid('deleteRow', rowIndex);
	}
}

function dictionaryClose(){
	$('#dictcatalogViewWindow').window('close');
}

function dictionarySave(){
	
	var rows = $('#dictionaryDataGrid').datagrid('getRows');
	for ( var i = 0; i < rows.length; i++) {
		$('#dictionaryDataGrid').datagrid('endEdit', i);
	}
	
	if ($('#dictionaryDataGrid').datagrid('getChanges').length) {
		var inserted = $('#dictionaryDataGrid').datagrid('getChanges', "inserted");
		var deleted = $('#dictionaryDataGrid').datagrid('getChanges', "deleted");
		var updated = $('#dictionaryDataGrid').datagrid('getChanges', "updated");
		var effectRow = new Object();
		
		if (inserted.length) {
			alert('inserted: ' + JSON.stringify(inserted));
			effectRow["inserted"] = JSON.stringify(inserted);
			
		}
		if (deleted.length) {
			alert('deleted: ' + JSON.stringify(deleted));
			effectRow["deleted"] = JSON.stringify(deleted);
		}
		if (updated.length) {
			alert('updated: ' + JSON.stringify(updated));
			effectRow["updated"] = JSON.stringify(updated);
		}
		
		$.post("dictionaryAjax!save", effectRow,
				function(data) {
					$.messager.alert("提示", "保存成功!");
					$('#dictionaryDataGrid').datagrid('reload');
		}, "json").error(function() {
			$.messager.alert("提示", "保存失败!");
		});
	}
}
